package com.entertainment.ticketing.controller;

import com.entertainment.ticketing.dto.UserDTO;
import com.entertainment.ticketing.dto.UserRegisterRequest;
import com.entertainment.ticketing.dto.UserUpdateProfileRequest;
import com.entertainment.ticketing.service.UserService;
import jakarta.validation.Valid;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/api/users")
public class UserController {
    private final UserService userService;

    public UserController(UserService userService) {
        this.userService = userService;
    }

    @GetMapping("/me")
    public ResponseEntity<UserDTO> getProfile(Authentication auth) {
        String username = (String) (auth != null ? auth.getPrincipal() : null);
        if (username == null) {
            return ResponseEntity.status(org.springframework.http.HttpStatus.UNAUTHORIZED).build();
        }
        Long userId = userService.getUserIdByUsername(username);
        if (userId == null) {
            return ResponseEntity.status(org.springframework.http.HttpStatus.UNAUTHORIZED).build();
        }
        return ResponseEntity.ok(userService.getProfile(userId));
    }

    @PostMapping("/register")
    public ResponseEntity<UserDTO> register(@Valid @RequestBody UserRegisterRequest req) {
        return ResponseEntity.ok(userService.register(req));
    }

    @PutMapping("/me")
    public ResponseEntity<UserDTO> updateProfile(Authentication auth, @Valid @RequestBody UserUpdateProfileRequest req) {
        String username = (String) (auth != null ? auth.getPrincipal() : null);
        if (username == null) {
            return ResponseEntity.status(org.springframework.http.HttpStatus.UNAUTHORIZED).build();
        }
        Long userId = userService.getUserIdByUsername(username);
        if (userId == null) {
            return ResponseEntity.status(org.springframework.http.HttpStatus.UNAUTHORIZED).build();
        }
        return ResponseEntity.ok(userService.updateProfile(userId, req));
    }

    @PostMapping("/me/avatar")
    public ResponseEntity<UserDTO> uploadAvatar(Authentication auth, @RequestParam("file") MultipartFile file) {
        String username = (String) (auth != null ? auth.getPrincipal() : null);
        if (username == null) {
            return ResponseEntity.status(org.springframework.http.HttpStatus.UNAUTHORIZED).build();
        }
        Long userId = userService.getUserIdByUsername(username);
        if (userId == null) {
            return ResponseEntity.status(org.springframework.http.HttpStatus.UNAUTHORIZED).build();
        }
        return ResponseEntity.ok(userService.uploadAvatar(userId, file));
    }
}